package com.etc.controller;

import com.etc.domian.User;
import com.etc.domian.UserImage;
import com.etc.domian.Userinfo;
import com.etc.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * @Author kalista
 * @Description
 * @Date 2020/9/27  16:45
 **/
@Controller
public class UserController {


    @Autowired
    private UserRepository userRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @PostMapping("login")
    public String login(User user, HttpServletRequest request){
        List<User> userList = userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());
        if (userList != null && !userList.isEmpty()){
            HttpSession session = request.getSession();
            session.setAttribute("user",userList.get(0));
            return "redirect:/home";
        }else {
            request.setAttribute("message","用户名密码错误");
            return "login";
        }
    }

    @GetMapping("home")
    public String hodme(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user != null){
            Query query = new Query(Criteria.where("userId").is(user.getId().toString()));
            List<UserImage> userImages = mongoTemplate.find(query, UserImage.class);
            request.setAttribute("userlist",userImages);
            return "home";
        }else {
            request.setAttribute("message","请登录");
            return "login";
        }
    }

    @PostMapping("add")
    @ResponseBody
    public String add (String str,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user != null){
            Userinfo userinfo = new Userinfo();
            userinfo.setId(UUID.randomUUID().toString().replaceAll("-",""));
            userinfo.setUserId(user.getId().toString());
            userinfo.setUserinfo(str);
            Userinfo save = mongoTemplate.save(userinfo);
            return "success";
        }

        return "fail";
    }

    @GetMapping("getUserlist")
    @ResponseBody
    public Userinfo home (HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user != null) {
            Query query = new Query(Criteria.where("userId").is(user.getId().toString()));
            Userinfo userImages = mongoTemplate.findOne(query, Userinfo.class);
           return userImages;
        }

        return null;
    }
}
